package com.campsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campsys.core.TableData;
import com.campsys.domain.TCourse;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.vo.CoursePageVo;
import com.campsys.mapper.ActivityMapper;
import com.campsys.mapper.ClazzMapper;
import com.campsys.mapper.CourseMapper;
import com.campsys.mapper.TeacherMapper;
import com.campsys.mapping.CourseMapping;
import com.campsys.service.ICourseService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper,TCourse> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public TableData getCoursePage(PageParam pageParam, String name, Long clazzId) {
        LambdaQueryWrapper<TCourse> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtil.isNullOrEmpty(name),TCourse::getCourseName,name);
        qw.ne(TCourse::getStatus,3);
        qw.eq(clazzId != null,TCourse::getClassId,clazzId);

        Page<TCourse> coursePage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());

        Page<TCourse> page = courseMapper.selectPage(coursePage,qw);

        List<CoursePageVo> list = CourseMapping.INSTANCE.toPage(page.getRecords());

        for (CoursePageVo coursePageVo : list){
            coursePageVo.setActivityName(activityMapper.selectById(coursePageVo.getActivityId()).getName());
            coursePageVo.setClassName(clazzMapper.selectById(coursePageVo.getClassId()).getName());
            coursePageVo.setTeacherName(teacherMapper.selectById(coursePageVo.getTeacherId()).getTeacherName());
        }

        return TableData.success(list,page.getTotal());
    }
}
